<template>
  <div class="fund-search-page">
    <!-- 顶部标题栏 -->
    <div class="header-bar">
      <div class="header-content">
        <h1 class="main-title">基金成绩单（股票型基金版）</h1>
        <div class="user-avatar">
          <img src="../assets/duck-avatar.png" alt="用户头像" />
    </div>
      </div>
    </div>
    
    <!-- 搜索区域 -->
    <div class="search-container">
      <el-input 
        v-model="fundCode" 
        placeholder="请输入基金代码（例如000595）" 
        class="search-input"
        clearable
      />
      <div class="example-codes">
        <span v-for="code in exampleCodes" :key="code" @click="fundCode = code">{{ code }}</span>
      </div>
      <div class="action-buttons">
        <el-button type="primary" @click="searchFund" class="search-btn">分析基金</el-button>
        <el-button @click="localMode" class="local-btn">本地模式，查询历史</el-button>
      </div>
    </div>
    
    <!-- 加载中 -->
    <div v-if="loading" class="loading-container">
      <p>正在连接{{ fundCode }}的数据，请稍等</p>
      </div>
    
    <!-- 错误提示 -->
    <div v-if="error" class="error-container">
      <p>或者您可以 <span class="link-text" @click="localMode">查看我以前看过的基金【基金】聊聊天</span></p>
          </div>
    
    <!-- 问答区域 -->
    <div v-if="showChatSection" class="chat-section">
      <div class="chat-content">
        <p>怎么判断基金是否值得投资？</p>
        <div class="chat-buttons">
          <button @click="askQuestion('这个行情下，怎么选？')">这个行情下，怎么选？</button>
          <button @click="askQuestion('专业基金经理看看？')">专业基金经理看看？</button>
        </div>
      </div>
    </div>
    
    <!-- AI助手图标 -->
    <div class="ai-assistant">
      <img src="../assets/duck-avatar.png" alt="用户助手" />
      <div class="assistant-label">AI助手</div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()
const fundCode = ref('')
const loading = ref(false)
const error = ref(false)
const showChatSection = ref(true)
const exampleCodes = ['000746', '019457', '015642']

// 提问
const askQuestion = (question) => {
  ElMessage.info('您提问了：' + question)
}

// 搜索基金
const searchFund = () => {
  if (!fundCode.value) {
    ElMessage.warning('请输入基金代码')
    return
  }
  
  loading.value = true
  error.value = false
  
  // 导航到基金成绩单页面
  router.push({ name: 'fund-score-card', params: { fundCode: fundCode.value } })
}

// 本地模式
const localMode = () => {
  router.push({ name: 'home' })
}

onMounted(() => {
  // 页面加载时自动进入本地模式
  localMode()
})
</script>

<style scoped>
.fund-search-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #fff;
}

.header-bar {
  background-color: #8086fd;
  color: white;
  padding: 15px 0;
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.main-title {
  font-size: 18px;
  font-weight: 500;
  margin: 0;
  text-align: center;
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  overflow: hidden;
  position: absolute;
  right: 20px;
  top: 50%;
  transform: translateY(-50%);
}

.user-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.search-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  max-width: 650px;
  margin: 50px auto 0;
  padding: 0 20px;
  width: 100%;
}

.search-input {
  width: 100%;
}

.search-input :deep(.el-input__inner) {
  height: 40px;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  font-size: 14px;
}

.example-codes {
  display: flex;
  gap: 15px;
  margin-top: 10px;
  width: 100%;
  justify-content: flex-start;
}

.example-codes span {
  font-size: 13px;
  color: #8086fd;
  cursor: pointer;
}

.example-codes span:hover {
  text-decoration: underline;
}

.action-buttons {
  display: flex;
  gap: 15px;
  margin-top: 20px;
  width: 100%;
}

.search-btn, .local-btn {
  flex: 1;
  height: 40px;
}

.search-btn {
  background-color: #8086fd;
  border-color: #8086fd;
}

.local-btn {
  background-color: #f0f0f0;
  color: #606266;
  border-color: #f0f0f0;
}

.loading-container, .error-container {
  text-align: center;
  margin-top: 40px;
  color: #666;
}

.link-text {
  color: #8086fd;
  cursor: pointer;
}

.link-text:hover {
  text-decoration: underline;
}

.chat-section {
  display: flex;
  justify-content: center;
  margin-top: 80px;
}

.chat-content {
  text-align: center;
}

.chat-content p {
  font-size: 16px;
  margin-bottom: 20px;
  color: #333;
}

.chat-buttons {
  display: flex;
  gap: 15px;
  justify-content: center;
}

.chat-buttons button {
  padding: 8px 16px;
  background-color: #f5f5f5;
  border: none;
  border-radius: 20px;
  color: #606266;
  cursor: pointer;
  transition: all 0.2s;
  font-size: 14px;
}

.chat-buttons button:hover {
  background-color: #eaecff;
  color: #8086fd;
}

.ai-assistant {
  position: fixed;
  bottom: 30px;
  right: 30px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.ai-assistant img {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  box-shadow: 0 3px 12px rgba(0, 0, 0, 0.15);
  cursor: pointer;
}

.assistant-label {
  margin-top: 5px;
  font-size: 12px;
  color: #606266;
}

@media (max-width: 576px) {
  .action-buttons {
    flex-direction: column;
  }
  
  .search-btn, .local-btn {
    width: 100%;
  }
  
  .chat-buttons {
    flex-direction: column;
  }
}
</style>